<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <TagTree class="w-1/4 xl:w-1/5" @select="handlPhotoTagTreeSelected" />
    <BasicTable class="w-3/4 xl:w-4/5" @register="registerTable">
      <template #toolbar>
        <!--    新增按钮    -->
        <a-button v-if="hasPermission('PList:create')" @click="handleInsert" type="primary">
          新增
        </a-button>
      </template>
    </BasicTable>
  </PageWrapper>

  <!--  编辑侧边抽屉  -->
  <PhotoUpdateDrawer @register="registerUpdateDrawer" @success="handleSuccess" />
</template>
<script lang="ts" setup>
  import TagTree from '@/views/photo/List/TagTree.vue';
  import { columns } from './data';
  import { useDrawer } from '@/components/Drawer';
  import { BasicTable, useTable } from '@/components/Table';
  import { reactive } from 'vue';
  import { PageWrapper } from '@/components/Page';
  import { hasPermission } from '@/utils/auth';
  import { listPhotoApi } from '@/api/photo/photoApi';
  import PhotoUpdateDrawer from './update-drawer.vue';
  import { usePhotoTagSelectOptions } from '@/api/select-options/hooks/useSelectOptions';
  // 新增/编辑
  const [registerUpdateDrawer, { openDrawer: openUpdateDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    title: '照片列表',
    api: listPhotoApi,
    formConfig: {
      /*
      列表查询条件
       */
      // 输入框左侧标题的宽度
      labelWidth: 120,
      // 查询条件配置
      schemas: undefined,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    columns: columns,
    actionColumn: {
      width: 130,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: undefined,
    },
    rowSelection: {
      type: 'checkbox',
    },
  });

  // 除了formConfig.schemas之外的搜索条件
  const searchInfo = reactive<Recordable>({
    selectedTagId: null,
  });

  //照片标签树点击事件
  function handlPhotoTagTreeSelected(tagId = '') {
    searchInfo.selectedTagId = tagId;
    reload();
  }

  /**
   * 编辑成功后事件
   */
  function handleSuccess() {
    reload();
  }
  /**
   *  照片标签预加载
   */
  const { photoTagSelectOptions, fetchPhotoTagSelectOptions } = usePhotoTagSelectOptions();
  fetchPhotoTagSelectOptions();
  /**
   * 新增图片侧边框
   */
  function handleInsert() {
    openUpdateDrawer(true, {
      isUpdateView: false,
      photoTagSelectOptions,
      selectedTagId: searchInfo.selectedTagId,
    });
  }
</script>
